/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.bus.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.bus.entity.VaccineEnt;
import com.thinkgem.jeesite.modules.bus.entity.VaccineManage;
import com.thinkgem.jeesite.modules.bus.service.VaccineEntService;
import com.thinkgem.jeesite.modules.bus.service.VaccineManageService;
import com.thinkgem.jeesite.modules.sys.entity.Dict;
import com.thinkgem.jeesite.modules.sys.utils.DictUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.bus.entity.SubsidyStandard;
import com.thinkgem.jeesite.modules.bus.service.SubsidyStandardService;

import java.util.List;

/**
 * 补助标准Controller
 * @author sy
 * @version 2019-06-11
 */
@Controller
@RequestMapping(value = "${adminPath}/bus/subsidyStandard")
public class SubsidyStandardController extends BaseController {

	@Autowired
	private SubsidyStandardService subsidyStandardService;
	@Autowired
	private VaccineEntService vaccineEntService;
	@Autowired
	private VaccineManageService vaccineManageService;
	
	@ModelAttribute
	public SubsidyStandard get(@RequestParam(required=false) String id) {
		SubsidyStandard entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = subsidyStandardService.get(id);
		}
		if (entity == null){
			entity = new SubsidyStandard();
		}
		return entity;
	}
	
	/**
	 * 
	 * @Title: list
	 * @author: sy
	 * @date: 2019年06月15日 13:26
	 * @Description: 补助标准列表
	 * @param subsidyStandard
	 * @param request
	 * @param response
	 * @param model
	 * @return: java.lang.String
	 */
	@RequiresPermissions("bus:subsidyStandard:view")
	@RequestMapping(value = {"list", ""})
	public String list(SubsidyStandard subsidyStandard, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SubsidyStandard> page = subsidyStandardService.findPage(new Page<SubsidyStandard>(request, response), subsidyStandard); 
		model.addAttribute("page", page);
		return "modules/bus/subsidyStandardList";
	}

	/**
	 * 
	 * @Title: form
	 * @author: sy
	 * @date: 2019年06月14日 15:43
	 * @Description: 新增补助标准
	 * @param subsidyStandard
	 * @param model
	 * @return: java.lang.String
	 */
	@RequiresPermissions("bus:subsidyStandard:view")
	@RequestMapping(value = "form")
	public String form(SubsidyStandard subsidyStandard, Model model) {
		Dict dict=new Dict();
		dict.setType("vaccineType");
		List<Dict> list= DictUtils.getDictList("vaccineType");
		model.addAttribute("vaccineTypeList",list);
		model.addAttribute("entList",vaccineEntService.findList(new VaccineEnt()));
		if(null != subsidyStandard && StringUtils.isNotBlank(subsidyStandard.getId())){
			subsidyStandard.setEntList(subsidyStandardService.findEntListBySubsidyStandardId(subsidyStandard.getId()));
			model.addAttribute("subsidyStandard", subsidyStandard);
		}else{
			model.addAttribute("subsidyStandard", subsidyStandard);
		}
		return "modules/bus/subsidyStandardForm";
	}

	/**
	 *
	 * @Title: save
	 * @author: sy
	 * @date: 2019年06月11日 15:19
	 * @Description: 保存
	 * @param subsidyStandard
	 * @param model
	 * @param redirectAttributes
	 * @return: java.lang.String
	 */
	@RequiresPermissions("bus:subsidyStandard:edit")
	@RequestMapping(value = "save")
	public String save(SubsidyStandard subsidyStandard, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, subsidyStandard)){
			return form(subsidyStandard, model);
		}
		subsidyStandardService.save(subsidyStandard);
		addMessage(redirectAttributes, "保存补助标准成功");
		return "redirect:"+Global.getAdminPath()+"/bus/subsidyStandard/findSubsidyStandardList/?repage";
	}
	
	@RequiresPermissions("bus:subsidyStandard:edit")
	@RequestMapping(value = "delete")
	public String delete(SubsidyStandard subsidyStandard, RedirectAttributes redirectAttributes) {
		subsidyStandardService.delete(subsidyStandard);
		addMessage(redirectAttributes, "删除补助标准成功");
		return "redirect:"+Global.getAdminPath()+"/bus/subsidyStandard/findSubsidyStandardList/?repage";
	}

	/**
	 *
	 * @Title: changeEnt
	 * @author: sy
	 * @date: 2019年06月15日 12:36
	 * @Description: 查询厂家是否已经存过该疫苗
	 * @param subsidyStandard
	 * @param model
	 * @param redirectAttributes
	 * @return: java.lang.String
	 */
	@ResponseBody
	@RequestMapping(value = "changeEnt")
	public String changeEnt(SubsidyStandard subsidyStandard, Model model, RedirectAttributes redirectAttributes) {
		return subsidyStandardService.changeEnt(subsidyStandard);
	}

	/**
	 *
	 * @Title: formValidate
	 * @author: sy
	 * @date: 2019年06月15日 14:51
	 * @Description: 验证适用企业为全部企业时，疫苗是否存在
	 * @param subsidyStandard
	 * @param model
	 * @param redirectAttributes
	 * @return: java.lang.String
	 */
	@ResponseBody
	@RequestMapping(value = "formValidate")
	public String formValidate(SubsidyStandard subsidyStandard, Model model, RedirectAttributes redirectAttributes) {
		return subsidyStandardService.formValidate(subsidyStandard);
	}
	/*
	 * @MethodName: findSubsidyStandardList
	 * @Description: 疫苗补助列表
	 * @Param: subsidyStandard,request,response,model
	 * @Return: String
	 * @Author: my
	 * @Date: 2019/7/23
	**/
	@RequiresPermissions("bus:subsidyStandard:view")
	@RequestMapping(value = "findSubsidyStandardList")
	public String findSubsidyStandardList(SubsidyStandard subsidyStandard, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<SubsidyStandard> page = subsidyStandardService.findSubsidyStandardList(new Page<SubsidyStandard>(request, response), subsidyStandard);
		model.addAttribute("page", page);
		return "modules/bus/subsidyStandardList";
	}
}